// 2013-8-1下午12:43:49  net.betou.trade.main.dao.impl.UserDaoImpl.java

package net.betou.trade.main.dao.impl;

import java.util.List;

import net.betou.common.hibernate3.Finder;
import net.betou.common.hibernate3.HibernateBaseDao;
import net.betou.common.page.Pagination;
import net.betou.common.util.DataConvertUtils;
import net.betou.pojo.Report;
import net.betou.trade.entity.main.User;
import net.betou.trade.main.dao.UserDao;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

/**
 * 
 * @author 叶装装
 */
@Repository
public class UserDaoImpl extends HibernateBaseDao< User, Integer > implements UserDao{

	@Override
	protected Class< User > getEntityClass( ) {
		return User.class;
	}

	public int countDemoAccount( String demoAccount ){
		return count( "demoAccount", demoAccount );
	}
	 
	 public int countFirmAccount( String firmAccount ){
		 return count( "firmAccount", firmAccount );
	 }
	 
	public int countByEmail( String email ) {
		return count( "email", email );
	}

	public int countByMobile( String mobile ) {
		return count( "mobile", mobile );
	}

	public int countByUsername( String username ) {
		return count( "username", username );
	}

	public User deleteById( Integer id ) {
		User bean = get( id );
		if( bean != null ){
			getSession( ).delete( bean );
		}
		return bean;
	}

	public User findById( Integer id ) {
		return get( id );
	}

	public User findByUsername( String username ) {
		return findUniqueByProperty( "username", username );
	}
	
	 public User findByEmail( String email ){
		 return findUniqueByProperty( "email", email );
	 }
	 
	 public User findByMobile( String mobile ){
		 return findUniqueByProperty( "mobile", mobile );
	 }

	public Pagination getPage( Integer selfId, String searchProperty, String searchValue, String orderProperty, String orderDirection, int pageNo, int pageSize, String filterStatus ) {
		Finder f = Finder.create( "select bean from User bean where 1=1 " );

		if( selfId != null ){
			f.append( " and bean.admin.id =:selfId " );
			f.setParam( "selfId", selfId );
		}

		createFilter( f, filterStatus );
		createSearch( f, searchProperty, searchValue );
		createOrder( f, orderProperty, orderDirection, "id" );
		
		return find(f, pageNo, pageSize);
	}
	
	@SuppressWarnings( "unchecked" )
	public List< String > getMobileList( Integer adminId, String [ ] groups ){
		
		Finder f = Finder.create( "select mobile from User bean where bean.admin.id=:adminId and ( " );
		f.setParam( "adminId", adminId );
		
		for( int i = 0; i < groups.length; i++ ){
			f.append( "bean.group.id=:g" + i );
			
			if( i < groups.length - 1 ){
				f.append( " or " );
			}
			
			f.setParam( "g" + i, DataConvertUtils.getInt( groups[ i ] ) );
		}
		
		f.append( ")" );
		
		return find( f, true );
	}

	public User save( User bean ) {
		getSession( ).save( bean );
		return bean;
	}
	
	 public User findMyUser( Integer userid, Integer adminId ){
		 String hql = "from User bean where id=? and bean.admin.id=?";
		 return ( User ) findUnique( hql, userid, adminId );
	 }
	 
	 public User findByQuery( String username ){
		 String hql = "from User bean where bean.username=? or bean.mobile=? or bean.email=?";
		 return ( User ) findUnique( hql, username, username, username );
	 }

	private int count( String name, String value ) {
		String hql = "select count(*) from User bean where bean." + name + "=:" + name;
		Query query = getSession( ).createQuery( hql );
		query.setParameter( name, value );
		return ( ( Number ) query.iterate( ).next( ) ).intValue( );
	}
	
	@SuppressWarnings( "unchecked" )
	public List< Report > findDayReport( String day ){
		String hql = "select new net.betou.pojo.Report(count(*), hour( bean.regDate ) ) from User bean where date_format(bean.regDate,'%Y-%m-%d') = ?  group by hour(bean.regDate) ";
		Query query = createQuery( hql, day );
		return query.list( );
	}
	
	@SuppressWarnings( "unchecked" )
	 public List< Report > findMonthReport( String month ){
		 String hql = "select new net.betou.pojo.Report(count(*), month( bean.regDate ) ) from User bean where date_format(bean.regDate,'%Y-%m') = ?  group by month(bean.regDate) ";
		Query query = createQuery( hql, month );
		return query.list( );
	 }
	
	/**
	 * @param year
	 * @return
	 */
	@SuppressWarnings( "unchecked" )
	public List< String > findReportChoice( String year, String format, String group ){
		
		String hql = "select date_format(bean.regDate,'" + format + "') from User bean where year(bean.regDate)='" + year + "' group by " + group + "(bean.regDate)";
		
		if( StringUtils.isBlank( year ) ){
			hql = "select year(bean.regDate) from User bean group by year(bean.regDate)";
		}
		
		 Query query = createQuery( hql );
		 return query.list( );
		
	}
}
